<template>
  <template v-for="field in searchFields" v-show="isNotEmpty(searchFields)">
    <SearchItemWrapper :searchField="field"/>
  </template>
</template>
<script setup>
import {isEmpty, isNotEmpty} from '@/utils/CommonUtils';
import SearchItemWrapper from '@/components/platwork/DefaultFormListComponent/SearchItemWrapper.vue';
import {provide} from 'vue';

let searchWrapperData = undefined;
provide('searchWrapperData', searchWrapperData = {
  searchData: {
    eq: [],
    like: [],
    in: [],
  },
  searchCallBacks: [],
  resetFieldMethods: []
});

const props = defineProps({
  searchFields: {
    type: Array,
    default: () => [],
  },
});

function getFieldSearchData() {
  clearSearchWrapperData();
  let callBack = searchWrapperData.searchCallBacks;
  if (isEmpty(callBack)) return;
  callBack.forEach(call => call());

  return searchWrapperData.searchData;
}

function clearSearchWrapperData() {
  searchWrapperData.searchData.eq = [];
  searchWrapperData.searchData.like = [];
  searchWrapperData.searchData.in = [];
  searchWrapperData.searchData.le = [];
  searchWrapperData.searchData.ge = [];
}

function reset() {
  let methods = searchWrapperData.resetFieldMethods;
  if (isEmpty(methods)) return;
  methods.forEach(method => method());
}

defineExpose({
  getFieldSearchData,
  reset
});
</script>
